home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 2
/
Atari Mega Archive CD - Volume 2.iso
/
linux
/
docs
/
ddtut.txt
< prev
Wrap
Text File
|
1995-04-22
|
43KB
|
1,136 lines
************************************************************
* *
* Tutorial To Linux Driver Writing -- Character Devices *
* *
* or, *
* *
* Now That I'm Wacky, Let Me Do Something (I) *
* *
* Last Revision: Apr 11, 1993 *
* *
************************************************************
This document (C) 1993 Robert Baruch. This document may be freely
copied as long as the entire title, copyright, this notice, and all of
the introduction are included along with it. Suggestions, criticisms,
and comments to baruch@nynexst.com. This document, nor the work
performed by Robert Baruch using Linux, nor the results of said work
are connected in any way to any of the Nynex companies. This product
0% organic as defined by California Statute 4Z//7&A. No artificial
coloring or flavoring.
========================
Introduction
========================
There is a companion guide to this Tutorial, the Guide to Linux Driver
Writing -- Character Devices This Guide should serve as a reference to
both beginning and advanced driver writers, and should be used in
conjunction with this Tutorial.
-=-=-=-=-=-=-
Some words of thanks:
Many thanks to:
Donald J. Becker (becker@metropolis.super.org)
Don "May the Source be With You!" Holzworth (donh@gcx1.ssd.csd.harris.com)
Michael Johnson (johnsonm@stolaf.edu)
Karl Heinz Kremer (khk@raster.kodak.com)
Pat Mackinlay (mackinla@cs.curtin.edu.au)
...others too numerous to mention...
All the driver writers!
...and of course, Linus "That's LIN-uhks" Torvalds and all the guys who helped
develop Linux into a BLOODY KICKIN' O/S!
-=-=-=-=-=-=-
...and now a word of warning:
Messing about with drivers is messing with the kernel. Drivers are run
at the kernel level, and as such are not subject to scheduling. Further,
drivers have access to various kernel structures. Before you actually
write a driver, be *damned* sure of what you are doing, lest you end
up having to re-format your harddrive and re-install Linux!
The information in this Tutorial is as up-to-date as I could make it. It also
has no stamp of approval whatsoever by any of the designers of the kernel.
I am not responsible for damage caused to anything as a result of using this
Guide.
========================
End of Introduction
========================
CHAPTRE THE FIRSTE : How did *they* get the device driver in the kernel?
------------------
You have to realize that device drivers really are part of the kernel. The
kernel can hook in to the functions in your device driver if you tell it
the addresses of some standard functions. These standard functions are
detailed in the Guide.
As a part of the kernel, the code of the device driver must be compiled in
*with* the kernel. That is, you must alter some Makefiles to compile your
driver and to get it archived into the chr_drv.a library, or you can
archive it yourself and link it in to the kernel at a later compile stage.
The first step, before you even write a single line of driver code, is to
make sure you know how to recompile the kernel. Then go ahead and actually
do it, to be sure you (and your system) are sane. Of course, you need
the sources to the kernel. If you have the SLS distribution of Linux, you
already have the sources in /linux. If you don't have the sources, you
can get it at one of these fine ftp sites near you:
tsx-11.mit.edu:/pub/linux
sunsite.unc.edu:/pub/Linux
Briefly, here's how to compile the kernel (at least this is how it's done
in the SLS release):
Go to /linux (or wherever the source for Linux is)
You will see a directory which looks a lot like this:
-rw-r--r-- 1 baruch 17982 Nov 10 07:54 COPYING
-rw-r--r-- 1 baruch 1444 Jan 13 15:24 Configure
-rw-r--r-- 1 baruch 6934 Feb 22 13:31 Makefile
-rw-r--r-- 1 baruch 4078 Dec 12 06:45 README
drwxrwxr-x 2 baruch 512 Feb 22 13:34 boot
-rw-r--r-- 1 baruch 1724 Feb 9 15:07 config.in
drwxrwxr-x 8 baruch 512 Feb 22 13:34 fs
drwxrwxr-x 4 baruch 512 Dec 1 19:40 include
drwxrwxr-x 2 baruch 512 Feb 22 13:34 init
drwxrwxr-x 5 baruch 512 Feb 9 15:11 kernel
drwxrwxr-x 2 baruch 512 Feb 9 15:11 lib
-rwxr-xr-x 1 baruch 166 Nov 10 07:54 makever.sh
drwxrwxr-x 2 baruch 512 Feb 22 13:34 mm
drwxrwxr-x 3 baruch 512 Feb 9 15:11 net
drwxrwxr-x 2 baruch 512 Feb 22 13:34 tools
drwxrwxr-x 2 baruch 512 Feb 22 13:34 zBoot
The README file should contain instructions, but here's how anyway:
Log in as root.
make clean (Do this only once. Otherwise you'll have to sit around
for 45 minutes or so while the whole thing recompiles)
make config (Answer the questions -- usually needed only the first time)
make dep (Makes dependencies)
make (makes the kernel)
You should end up with an Image file. This is the kernel. Put it where
you like (LILO users should take it from there). To make a bootable disk,
just pop a DOS formatted disk in drive A, and do:
make disk
------------------------------------------------------------------------
CHAPTER TWO: The simplest driver you've ever seen.
------------
Now, the directory you're interested in is <src>/kernel/chr_drv. This is
where all the character device drivers are kept. Go to that directory.
Open up a new file, and call it testdata.c. Here is what you should
put in it:
========================================
File Listing 1: testdata.c
========================================
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/tty.h>
#include <linux/signal.h>
#include <linux/errno.h>
#include <asm/io.h>
#include <asm/segment.h>
#include <asm/system.h>
#include <asm/irq.h>
unsigned long test_init(unsigned long kmem_start)
{
printk("Test Data Generator installed.\n");
return kmem_start;
}
========================================
The include files are all there for convenience. You may need them later.
All this driver does is upon initialization, display a message.
Now, to get this driver into the kernel, you need to do several things.
The first two things do in the chr_drv directory:
I. Get the kernel to call your init function on bootup. To do this,
edit the mem.c file, and go to the very end to the function
chr_drv_init. It looks something like this:
long chr_dev_init(long mem_start, long mem_end)
{
if (register_chrdev(1,"mem",&memory_fops))
printk("unable to get major 1 for memory devs\n");
mem_start = tty_init(mem_start);
mem_start = lp_init(mem_start);
mem_start = mouse_init(mem_start);
mem_start = soundcard_init(mem_start);
return mem_start;
}
You need to add your test_init function to the code. Put it right
before the return:
mem_start = test_init(mem_start);
Save the file.
II. Edit the Makefile to compile testdata.c. Edit the Makefile, and add
testdata.o to the OBJS list. This will cause the make utility to
compile testdata.c into an object file, and then add it to the
chr_drv.a library archive.
Save the file.
The next step is to re-compile the kernel. Go to the <src> directory,
and do a make from the top as described in the first chapter. There is
no point in doing a "make clean" or "make config". If all goes well, the
make should proceed down to chr_drv, and compile your testdata.c file.
If there are warnings or errors, do a ctrl-C to break out of the make,
and fix the problem.
Once you are left with an Image file, put the Image file where LILO
wants it, or use "make disk" to make a bootable disk. It's a good
idea to save your old Image file (or save the disk it was on).
Now reboot. When Linux comes up again, you should see your message
printed on bootup after all the char